jconed = {};

jconed._instances = {};
jconed._context_handlers = [];
jconed._loaders = [];
jconed._loaded = false;
jconed._evts_list = [];

jconed.PLUGIN_REM_START = "<!-- LIWE PLUGIN START -->";
jconed.PLUGIN_REM_END   = "<!-- LIWE PLUGIN END -->";
jconed.PLUGIN_START = "<!-- LIWE PLUGIN CONTENT START -->";
jconed.PLUGIN_END   = "<!-- LIWE PLUGIN CONTENT END -->";

jconed.get = function ( id )
{
	return jconed._instances.get ( id );
};

jconed.init = function ()
{
	jconed.em = new liwe.event_manager ( jconed );
	if ( jconed._loaded ) return;
	jconed.toolbar.init ();
	jconed._loaded = true;
};

jconed.replace = function ( css_rule, cback )
{
	var items = document.querySelectorAll ( css_rule );
	for ( var i = 0; i < items.length; i++ )
	{
		var item = items [ i ];
		if ( typeof ( item ) != 'object' ) return;
		var editor = new jconed.editor ( item );
		editor.cback [ 'confirm' ] = cback;
	}
};

jconed.execute = function ( action, value, evt_name )
{
	if ( ! jconed.editor._selection ) return;
	if ( action == 'insertNode' )
	{
		var range = jconed.editor._selection [ 0 ];
		if ( ! range.collapsed )
			range.deleteContents ();
		range.insertNode ( value );
	}
	else
	{
		var sel = window.getSelection ();
		sel.removeAllRanges ();
		for ( var i = 0; i < jconed.editor._selection.length; i++ )
			sel.addRange ( jconed.editor._selection [ i ] );
		document.execCommand ( action, false, value );
	}
	if ( evt_name )
		jconed.dispatchEvent ( evt_name, null, { 'value' : value, 'sel' : sel } );
};

jconed.rgb_to_hex = function ( color )
{
	if ( color.substr ( 0, 1 ) == '#')
		return color;
	var res = /rgb\((\d+), (\d+), (\d+)\)/.exec ( color );
	if ( ! res ) return '';

	var r = parseInt ( res [ 1 ], 10 );
	var g = parseInt ( res [ 2 ], 10 );
	var b = parseInt ( res [ 3 ], 10 );

	var rgb = b | ( g << 8 ) | ( r << 16 );
	return '#' + rgb.toString ( 16 ).toUpperCase ();
};

jconed.register_context_handler = function ( cback )
{
	jconed._context_handlers.push ( cback );
};

jconed.register_loader = function ( cback )
{
	jconed._loaders.push ( cback );
};

jconed.get_plugin_selection = function ()
{
	var selections = jconed.editor.get_selection ();
	var sel = selections [ 0 ];
	var editor_el = $( jconed.editor.current.id );

	if ( selections.length > 1 )
		var item = sel.startContainer.childNodes [ sel.startOffset ];
	else
	{
		if ( sel.startContainer == editor_el )
			var item = sel.startContainer.childNodes [ sel.startOffset ];
		else
			var item = sel.startContainer;
	}

	while ( item && ! liwe.dom.has_class ( item, 'liwe-plugin' ) && item != editor_el )
		item = item.parentNode;

	if ( ! liwe.dom.has_class ( item, 'liwe-plugin' ) ) return false;

	return item;
};

jconed.render_plugin = function ( form_name )
{
	var f = liwe.form.get ( form_name );
	if ( ! f.check () ) return;
	var values = f.get_values ();

	var title = values [ '_title' ];
	var module = values [ '_module' ];

	delete values [ '_title' ];
	delete values [ '_module' ];

	var json = {
		"module": module,
		"params": values
	};

	var dict = {
		"title": title,
		"json": json.toJSONString ()
	};

	var tpl = jconed.PLUGIN_REM_START + 
		'<div class="code"><span>%(title)s</span><pre>%(json)s</pre></div>' + jconed.PLUGIN_START + '<div class="content"></div>' + jconed.PLUGIN_END +
		jconed.PLUGIN_REM_END;

	var sel = jconed.get_plugin_selection ();

	//if ( sel.get ( 'id' ) == values [ '_div_id' ] )
	if ( liwe.dom.has_class ( sel, 'liwe-plugin-' + module ) )
	{
		var items = sel.getElementsByTagName ( 'pre' );
		var pre = items [ 0 ];
		pre.innerHTML = dict [ 'json' ];
		liwe.dom.del_class ( sel, 'rendered' );
	}
	else
	{
		var div = document.createElement ( 'div' );
		div.className = 'liwe-plugin liwe-plugin-' + module;
		div.contentEditable = false;
		//div.id = values [ '_div_id' ];
		div.innerHTML = String.formatDict ( tpl, dict );

		jconed.execute ( 'insertNode', div );
	}
};
